package com.teradata.dashboard;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.teradata.dashboard.constant.DashboardConstant;
import com.teradata.tpif.common.util.string.StringUtil;
//import com.teradata.tpif.common.util.string.StringUtil;

import android.app.Application;

import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

public class DashboardApplication extends Application {

	public static DashboardApplication app;
	private Map shareData;
	private CookieManager cookieManager;
	private CookieSyncManager cookieSyncManager;
	private String userId = "";
	private String nickname = "";
	private String password = "";
	private String myCookie;

	
	
	public String getMyCookie() {
		return myCookie;
	}

	public void setMyCookie(String myCookie) {
		this.myCookie = myCookie;
	}

	@Override
	public void onCreate()
	{
		app = this;
		super.onCreate();
		initConstant();
		initCache();

	}
	
	public void intiCookie()
	{
		System.out.println("---------------初始化cookie前");
		initCookieManager();
	}

	private void initCache() {
		shareData = new HashMap();
	}

	private void initConstant() {
		
	}

	@Override
	public void onLowMemory()

	{

		super.onLowMemory();
		initCache();
	}

	public boolean isLogin()

	{
		System.out.println("--------isLogin?-----------");
		boolean bool = true;

		if (StringUtil.isNullOrEmpty(this.myCookie)) {
			
			bool = false;
		}
		System.out.println("--------isLogin?-----------" + bool+"--");
		return bool;
	}

	private void initCookieManager()

	{

		this.cookieSyncManager = CookieSyncManager.createInstance(this);

		this.cookieManager = CookieManager.getInstance();

		this.cookieManager.setAcceptCookie(true);

		if (isLogin())

		{
			String str = this.cookieManager.getCookie(DashboardConstant
					.getPara("COOKIEURL", ""));
			System.out.println("jingrulehaha" + str);
			if ((str == null) || (!str.contains("myCookie")) || 
					(!str.contains("userId")))

			{
				setCookie(DashboardConstant.AUTH_KEY_COOKIE_NAME,
						this.myCookie);
				setCookie(DashboardConstant.AUTH_KEY_COOKIE_USERID,
						this.userId);
			}
			showCookie();
			
		}
	}
	
	 private void showCookie()
	  {
	    String str = this.cookieManager.getCookie(DashboardConstant
				.getPara("COOKIEURL", ""));
	    System.out.println("cookie:" + str);
	  }

	public CookieManager getCookieManager() {
		return cookieManager;
	}

	public void setCookieManager(CookieManager cookieManager) {
		this.cookieManager = cookieManager;
	}

	public CookieSyncManager getCookieSyncManager() {
		return cookieSyncManager;
	}

	public void setCookieSyncManager(CookieSyncManager cookieSyncManager) {
		this.cookieSyncManager = cookieSyncManager;
	}

	private void setCookie(String cookieName, String cookieValue)

	{

		Calendar localCalendar = Calendar.getInstance();

		localCalendar.set(2025, 11, 29);

		String str = localCalendar.getTime().toString();

		this.cookieManager.setCookie(
				DashboardConstant.getPara("COOKIEURL", ""), cookieName + "="
						+ cookieValue + "; path=tpif;domain=101.227.243.40;expires=" + str);
		this.cookieSyncManager.sync();
	}

	private void removeCookie()
	{
		this.cookieManager.removeSessionCookie();

		this.cookieManager.removeAllCookie();

		this.cookieSyncManager.sync();
	}

	public Map getShareData() {
		return shareData;
	}

	public void setShareData(Map shareData) {
		this.shareData = shareData;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String username) {
		this.userId = username;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}



	
	
}
